package com.legrand.core.exception;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * @author tomaer <i@tomaer.com> Created Reponse.java on 4:47:40 PM
 * @version 1.0
 */
@JsonIgnoreProperties("statusCode")
public final class Response implements Exceptions, Serializable {

	private static final long serialVersionUID = -8247197287759168676L;
	
	int statusCode;
	String code;
	String devMessage;
	String usrMessage;
	String stack;
	Date timestamp;
	
	public static Response COMMON_UNKONW_SYSTEM_EXCEPTION = buildResponse(500, "0050001", "UNKonw System Error","未知的系统错误");
	public static Response COMMON_UNAUTHORIZED_EXCEPTION = buildResponse(500,"0040001","UNAUTHORIZED","认证失败,请重新获取Token");
	public static Response COMMON_ILLEGALREQUESTARGUMENT_EXCEPTION = buildResponse(500, "0040002", "Illegal Request Argument");
	public static Response COMMON_NOT_FOUND_EXCEPTION = buildResponse(500, "0040003", "Resource Not Found");
	
	private Response() {
	}

	private Response(final int statusCode, final String code) {
		this(statusCode, code, StringUtils.EMPTY);
	}

	private Response(final int statusCode, final String code, final String devMessage) {
		this(statusCode, code, devMessage, StringUtils.EMPTY);
	}

	private Response(final int statusCode, final String code, final String devMessage, final String usrMessage) {
		this(statusCode, code, devMessage, usrMessage, StringUtils.EMPTY);
	}
	
	private Response(final int statusCode, final String code, final String devMessage, final String usrMessage, final String stack) {
		this.statusCode = statusCode;
		this.code = code;
		this.devMessage = StringUtils.isNotBlank(devMessage) ? devMessage : null;
		this.usrMessage = StringUtils.isNotBlank(usrMessage) ? usrMessage : null;
		this.stack = StringUtils.isNotBlank(stack) ? stack : null;
		this.timestamp = new Date();
	}

	public static Response buildResponse(final int statusCode, final String code) {
		return new Response(statusCode, code);
	}

	public static Response buildResponse(final int statusCode, final String code, final String devMessage) {
		return new Response(statusCode, code, devMessage);
	}
	
	public static Response buildResponse(final int statusCode, final String code, final String devMessage, final String usrMessage) {
		return new Response(statusCode, code, devMessage, usrMessage);
	}
	
	public static Response buildResponse(final int statusCode, final String code, final String devMessage, final String usrMessage, final String stack) {
		return new Response(statusCode, code, devMessage, usrMessage, stack);
	}
	@Override
	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}
	@Override
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	@Override
	public String getDevMessage() {
		return devMessage;
	}
	@Override
	public void setDevMessage(String devMessage) {
		this.devMessage = devMessage;
	}
	@Override
	public String getUsrMessage() {
		return usrMessage;
	}
	@Override
	public void setUsrMessage(String usrMessage) {
		this.usrMessage = usrMessage;
	}
	@Override
	public String getStack() {
		return stack;
	}
	@Override
	public void setStack(String stack) {
		this.stack = stack;
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	
}
